package game

import (
	"encoding/json"
	"fmt"
	"gosum/server/src/csv"
	"io/ioutil"
	"os"
	"time"
)

//基础角色数据

type ModPlayer struct {
	UserId         int         //唯一ID
	Icon           int         //头像
	Card           int         //名片
	Name           string      //名字
	Sign           string      //签名
	PlayerLevel    int         //等级
	PlayerExp      int         //经验
	WorldLevel     int         //大世界等级
	WorldLevelNow  int         //大世界当前等级
	WorldLevelCool int64       //大世界刷新冷却
	Birth          int         //生日
	ShowTeam       []*ShowRole //展示阵容
	HideShowTeam   int         //是否展示阵容
	ShowCard       []int       //展示名片

	//封号
	Prohibit int
	//Gm
	IsGM int

	//装饰模式

	player *Player
	path   string
}

type ShowRole struct {
	RoleId    int
	RoleLevel int
}

//设置头像

func (self *ModPlayer) SetIcon(iconId int) {

	if !self.player.GetModIcon().IsHasIcon(iconId) {
		//没有图标 通知客户端操作非法
		//对应的报错代码
		fmt.Println("没有头像", iconId)
		return
	}

	self.Icon = iconId

	fmt.Println("当前头像： ", csv.GetItemName(iconId), self.Icon)
}

//设置名片

func (self *ModPlayer) SetCard(cardId int) {
	if !self.player.GetModCard().IsHasCard(cardId) {
		return
	}

	self.Card = cardId

	fmt.Println("当前名片： ", self.Card)
}

//设置名称

func (self *ModPlayer) SetName(name string) {

	if GetManageBanWord().IsBanWord(name) {
		//违规名字
		//对应的报错代码
		return
	}

	self.Name = name
	fmt.Println("当前用户名称： ", self.Name)
}

//设置签名

func (self *ModPlayer) SetSign(sign string, player *Player) {

	if GetManageBanWord().IsBanWord(sign) {
		//签名中有违规字符
		//对应报错代码
		return
	}

	self.Sign = sign
	fmt.Println("当前用户签名： ", self.Sign)
}

//获得经验/ 突破任务

func (self *ModPlayer) AddExp(exp int, player *Player) {
	self.PlayerExp += exp

	for {
		config := csv.GetNowLevelConfig(self.PlayerLevel)
		if config == nil {
			break
		}
		if config.PlayerExp == 0 {
			break
		}
		//是否完成突破任务
		if config.ChapterId > 0 && !player.GetModUniqueTask().IsTaskFinish(config.ChapterId) {
			break
		}

		//升级
		if self.PlayerExp >= config.PlayerExp {
			self.PlayerLevel += 1
			self.PlayerExp -= config.PlayerExp
		} else {
			break
		}
	}
	fmt.Println("当前等级: ", self.PlayerLevel, "当前经验: ", self.PlayerExp)

}

//降低 返回 世界等级

func (self *ModPlayer) ReduceWorldLevel() {
	if self.WorldLevel < csv.REDUCE_WORLD_LEVEL_START {
		fmt.Println("操作失败 当前世界等级", self.WorldLevel)
		return
	}
	if self.WorldLevel-self.WorldLevelNow >= csv.REDUCE_WORLD_LEVEL_MAX {
		fmt.Println("操作失败 当前世界等级", self.WorldLevel, "     真实世界等级:", self.WorldLevelNow)
		return
	}
	if time.Now().Unix() < self.WorldLevelCool {
		fmt.Println("操作失败：冷却中")
	}
	self.WorldLevelNow -= 1
	self.WorldLevelCool = time.Now().Unix() + csv.REDUCE_WORLD_LEVEL_COOL_TIME
	fmt.Println("操作成功 当前世界等级", self.WorldLevel, "     真实世界等级:", self.WorldLevelNow)
	return
}

func (self *ModPlayer) ReturnWorldLevel() {
	if self.WorldLevelNow == self.WorldLevel {
		fmt.Println("操作失败 当前世界等级", self.WorldLevel, "     真实世界等级:", self.WorldLevelNow)
		return
	}
	if time.Now().Unix() < self.WorldLevelCool {
		fmt.Println("操作失败：冷却中")
	}
	self.WorldLevelNow += 1
	self.WorldLevelCool = time.Now().Unix() + csv.REDUCE_WORLD_LEVEL_COOL_TIME
	fmt.Println("操作成功 当前世界等级", self.WorldLevel, "     真实世界等级:", self.WorldLevelNow)
	return
}

func (self *ModPlayer) SetBirth(birth int) {
	if self.Birth > 0 {
		fmt.Println("已经设置过你的生日了")
		return
	}
	//904
	month := birth / 100
	day := birth % 100

	switch month {
	case 1, 3, 5, 7, 8, 10, 12:
		if day <= 0 || day > 31 {
			fmt.Println(month, " 月 没有", day, "日")
			return
		}

	case 4, 6, 9, 11:
		if day <= 0 || day > 30 {
			fmt.Println(month, " 月 没有", day, "日")
			return
		}

	case 2:
		if day <= 0 || day > 29 {
			fmt.Println(month, " 月 没有", day, "日")
			return
		}

	default:
		fmt.Println("没有", month, "月")
		return
	}
	self.Birth = birth
	fmt.Println("设置成功 生日为:", month, "月", day, "日")

	if self.IsBirth() {
		fmt.Println("今天是你的生日 生日快乐")
	} else {
		fmt.Println("今天还不是你的生日")
	}
}

func (self *ModPlayer) IsBirth() bool {
	month := time.Now().Month()
	day := time.Now().Day()

	if int(month) == self.Birth/100 && int(day) == self.Birth%100 {
		return true
	}

	return false
}

func (self *ModPlayer) SetShowCard(showCard []int, player *Player) {

	if len(showCard) > csv.SHOW_SIZE {
		return
	}
	cardExistMap := make(map[int]int)
	newList := make([]int, 0)
	for _, cardId := range showCard {
		_, ok := cardExistMap[cardId]
		if ok {
			continue
		}
		if !player.GetModCard().IsHasCard(cardId) {
			continue
		}
		newList = append(newList, cardId)
		cardExistMap[cardId] = 1
	}

	self.ShowCard = newList
	fmt.Println(self.ShowCard)
}

func (self *ModPlayer) SetShowTeam(showRole []int, player *Player) {

	if len(showRole) > csv.SHOW_SIZE {
		return
	}
	roleExist := make(map[int]int)
	newList := make([]*ShowRole, 0)
	for _, roleId := range showRole {
		_, ok := roleExist[roleId]
		if ok {
			continue
		}
		if !player.GetModRole().IsHasRole(roleId) {
			continue
		}
		showRole := new(ShowRole)
		showRole.RoleId = roleId
		showRole.RoleLevel = player.GetModRole().GetRoleLevel(roleId)

		newList = append(newList, showRole)
		roleExist[roleId] = 1
	}
	self.ShowTeam = newList
	fmt.Println(self.ShowTeam)
}

func (self *ModPlayer) SetHideShowTeam(isHide int, player *Player) {
	if isHide != csv.LOGIC_TRUE && isHide != csv.LOGIC_FALSE {
		return
	}
	self.HideShowTeam = isHide
}

func (self *ModPlayer) SetProhibit(prohibit int) {
	self.Prohibit = prohibit
}

func (self *ModPlayer) SetIsGM(isGM int) {
	self.IsGM = isGM
}

func (self *ModPlayer) IsCanEnter() bool {
	return int64(self.Prohibit) < time.Now().Unix()
}

func (self *ModPlayer) GetWorldLevelNow() int {
	return self.WorldLevelNow
}

func (self *ModPlayer) RelicsUp(itemId int, player *Player) {
	player.GetModRelics().UpRelicsLevel(itemId, player)
}

func (self *ModPlayer) SaveData() {
	self.ShowCard = append(self.ShowCard, 1)
	self.ShowCard = append(self.ShowCard, 2)
	self.ShowCard = append(self.ShowCard, 3)
	self.ShowCard = append(self.ShowCard, 4)
	content, err := json.Marshal(self)
	if err != nil {
		return
	}
	ioutil.WriteFile(self.path, content, os.ModePerm)

}

func (self *ModPlayer) LoadData(player *Player) {

	self.player = player
	self.path = self.player.localPath + "/player.json"
	configFile, err := ioutil.ReadFile(self.path)
	if err != nil {
		fmt.Println("error")
		return
	}
	err = json.Unmarshal(configFile, &self)
	if err != nil {
		self.InitData()
		return
	}
	return
}
func (self *ModPlayer) InitData() {
	self.Name = "woyan"
	self.PlayerLevel = 1
	self.WorldLevel = 1
	self.WorldLevelNow = 1
	self.Icon = 0
	self.Card = 0
}
